package com.cgt.xiaotao.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cgt.xiaotao.product.domain.dto.PageDTO;
import com.cgt.xiaotao.product.domain.dto.ProductAdminDTO;
import com.cgt.xiaotao.product.domain.query.ProductPageQuery;
import com.cgt.xiaotao.product.domain.vo.ProductListVO;
import com.cgt.xiaotao.product.utils.SnowflakeIdGenerator;
import com.cgt.xiaotao.product.domain.po.Product;
import com.cgt.xiaotao.product.mapper.ProductMapper;
import com.cgt.xiaotao.product.service.IProductService;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    private final ProductMapper productMapper;
    private final SnowflakeIdGenerator idGenerator;

    @Override
    public PageDTO<ProductListVO> getPage(ProductPageQuery query) {
        Integer pageNo = query.getPageNo();
        Integer pageSize = query.getPageSize();
        int offset = (pageNo - 1) * pageSize;
        return PageDTO.of(
                productMapper.selectCount(null),
                productMapper.selectCount(null) / pageSize + 1,
                productMapper.selectByTags(query, offset, pageSize).stream()
                        .map(product -> new ProductListVO(
                                product.getId(),
                                product.getImageUrl(),
                                product.getName(),
                                product.getPrice(),
                                product.getSales(),
                                product.getComments(),
                                product.getIsAd()
                        ))
                        .collect(Collectors.toList())
        );
    }


    @Override
    public PageDTO<ProductAdminDTO> selectAdminPage(Integer pageNo, Integer pageSize, String sortBy, boolean isAsc) {

        Page<Product> page = new Page<>(pageNo, pageSize);

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        if (sortBy != null && !sortBy.isEmpty()) {
            if (isAsc) {
                queryWrapper.orderByAsc(sortBy);
            } else {
                queryWrapper.orderByDesc(sortBy);
            }
        }

        page = productMapper.selectPage(page, queryWrapper);

        List<ProductAdminDTO> list = page.getRecords().stream()
                .map(product -> {
                    ProductAdminDTO dto = new ProductAdminDTO();
                    dto.setId(product.getId());
                    dto.setName(product.getName());
                    dto.setImageUrl(product.getImageUrl());
                    dto.setPrice(product.getPrice());
                    dto.setSales(product.getSales());
                    dto.setComments(product.getComments());
                    dto.setIsAd(product.getIsAd());
                    dto.setStatus(product.getStatus());
                    dto.setInventory(product.getInventory());
                    dto.setTags(product.getTags());
                    return dto;
                })
                .collect(Collectors.toList());

        return new PageDTO<>(page.getTotal(), page.getPages(), list);
    }

    @Override
    public PageDTO<ProductAdminDTO> searchProduct(ProductPageQuery query) {

        if (query.getSortBy() == null || query.getSortBy().isEmpty()) {
            query.setSortBy("id");
        }
        if (query.getIsAsc() == null) {
            query.setIsAsc(true);
        }

        System.out.println( query);
        // 构建查询条件
        Page<Product> page = this.lambdaQuery()
                .like(query.getName() != null && !query.getName().isEmpty(), Product::getName, query.getName())
                .ge(query.getMinPrice() != null && query.getMinPrice().compareTo(BigDecimal.ZERO) > 0, Product::getPrice, query.getMinPrice())
                .le(query.getMaxPrice() != null && query.getMaxPrice().compareTo(BigDecimal.ZERO) > 0, Product::getPrice, query.getMaxPrice())

                .page(query.toMpPage(query.getSortBy(), query.getIsAsc()));
        List<Product> records = page.getRecords();

        List<ProductAdminDTO> list = records.stream().map(product -> {
            ProductAdminDTO dto = new ProductAdminDTO();
            dto.setId(product.getId());
            dto.setName(product.getName());
            dto.setImageUrl(product.getImageUrl());
            dto.setPrice(product.getPrice());
            dto.setSales(product.getSales());
            dto.setComments(product.getComments());
            dto.setIsAd(product.getIsAd());
            dto.setInventory(product.getInventory());
            dto.setTags(product.getTags());
            dto.setStatus(product.getStatus());
            return dto;
        }).collect(Collectors.toList());

        PageDTO<ProductAdminDTO> result = PageDTO.of(page.getTotal(), page.getPages(), list);
        return result;
    }

}
